<template>
  <div class="quality-template p-4">
    <!-- 页面标题和操作按钮 -->
    <div class="flex justify-between items-center mb-4">
      <h2 class="text-lg font-medium">质量检测模板</h2>
      <div class="flex gap-2">
        <el-button type="primary" @click="handleAdd">
          <div class="i-carbon-add mr-1" />
          添加
        </el-button>
        <el-button type="danger" :disabled="!selectedRows.length" @click="handleBatchDelete">
          <div class="i-carbon-trash-can mr-1" />
          删除
        </el-button>
      </div>
    </div>

    <!-- 模板列表 -->
    <div class="bg-white p-6 rounded-lg shadow-md">
      <ArtTable 
        :data="templateList" 
        :loading="loading"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="80" align="center" />
        <el-table-column prop="templateCode" label="模板编码" min-width="120" show-overflow-tooltip />
        <el-table-column prop="templateName" label="模板名称" min-width="200" show-overflow-tooltip />
        <el-table-column prop="ruleType" label="规则类型" width="120" align="center" />
        <el-table-column prop="useType" label="使用类型" width="120" align="center" />
        <el-table-column prop="description" label="描述" min-width="200" show-overflow-tooltip />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click="handleEdit(scope.row)">
              <div class="i-carbon-edit mr-1" />
              编辑
            </el-button>
            <el-button type="danger" link @click="handleDelete(scope.row)">
              <div class="i-carbon-trash-can mr-1" />
              删除
            </el-button>
          </template>
        </el-table-column>
      </ArtTable>

      <!-- 分页 -->
      <div class="flex justify-end mt-4">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 添加/编辑模板对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '添加规则模板' : '编辑规则模板'"
      width="600px"
      destroy-on-close
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
        label-position="right"
      >
        <el-form-item label="规则类型" prop="ruleType">
          <el-select v-model="formData.ruleType" placeholder="请选择" class="w-full">
            <el-option label="唯一性" value="唯一性" />
            <el-option label="完整性" value="完整性" />
          </el-select>
        </el-form-item>
        <el-form-item label="使用类型" prop="useType">
          <el-select v-model="formData.useType" placeholder="请选择" class="w-full">
            <el-option label="单字段" value="单字段" />
            <el-option label="多字段" value="多字段" />
          </el-select>
        </el-form-item>
        <el-form-item label="规则模板名称" prop="templateName">
          <el-input v-model="formData.templateName" placeholder="请输入规则模板名称" />
        </el-form-item>
        <el-form-item label="模板编码" prop="templateCode">
          <el-input v-model="formData.templateCode" placeholder="请输入模板编码" />
        </el-form-item>
        <el-form-item label="模板内容" prop="templateContent">
          <el-input
            v-model="formData.templateContent"
            type="textarea"
            :rows="4"
            placeholder="请输入模板内容"
          />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="formData.description"
            type="textarea"
            :rows="3"
            placeholder="请输入描述信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 删除确认对话框 -->
    <el-dialog
      v-model="deleteDialogVisible"
      title="提示"
      width="360px"
    >
      <div class="flex items-center gap-2">
        <div class="i-carbon-warning text-2xl text-yellow-500" />
        <span>此操作将永久删除该文件, 是否继续?</span>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="deleteDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmDelete">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

// 表格数据
const loading = ref(false)
const templateList = ref([
  {
    id: 1,
    templateCode: 'BWY',
    templateName: '检测不唯一',
    ruleType: '唯一性',
    useType: '单字段',
    description: '检测不唯一',
    templateContent: 'columnName in (select columnName from tableName group by columnName having count(*) > 1)'
  },
  {
    id: 2,
    templateCode: 'SJHBZQ',
    templateName: '检测手机号不正确',
    ruleType: '完整性',
    useType: '单字段',
    description: '检测手机号不正确',
    templateContent: 'columnName not regexp \'^1[3-9][0-9]{9}$\''
  }
])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 选中行
const selectedRows = ref([])

// 对话框相关
const dialogVisible = ref(false)
const dialogType = ref<'add' | 'edit'>('add')
const deleteDialogVisible = ref(false)
const deleteId = ref<number | null>(null)

// 表单相关
const formRef = ref<FormInstance>()
const formData = reactive({
  templateCode: '',
  templateName: '',
  ruleType: '',
  useType: '',
  templateContent: '',
  description: ''
})

// 表单校验规则
const formRules: FormRules = {
  ruleType: [{ required: true, message: '请选择规则类型', trigger: 'change' }],
  useType: [{ required: true, message: '请选择使用类型', trigger: 'change' }],
  templateName: [{ required: true, message: '请输入规则模板名称', trigger: 'blur' }],
  templateCode: [{ required: true, message: '请输入模板编码', trigger: 'blur' }],
  templateContent: [{ required: true, message: '请输入模板内容', trigger: 'blur' }]
}

// 处理表格选择
const handleSelectionChange = (rows: any[]) => {
  selectedRows.value = rows
}

// 处理分页
const handleSizeChange = (val: number) => {
  pageSize.value = val
  // TODO: 重新加载数据
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  // TODO: 重新加载数据
}

// 添加模板
const handleAdd = () => {
  dialogType.value = 'add'
  dialogVisible.value = true
  // 重置表单
  Object.assign(formData, {
    templateCode: '',
    templateName: '',
    ruleType: '',
    useType: '',
    templateContent: '',
    description: ''
  })
}

// 编辑模板
const handleEdit = (row: any) => {
  dialogType.value = 'edit'
  dialogVisible.value = true
  // 填充表单数据
  Object.assign(formData, row)
}

// 删除模板
const handleDelete = (row: any) => {
  deleteId.value = row.id
  deleteDialogVisible.value = true
}

// 批量删除
const handleBatchDelete = () => {
  if (!selectedRows.value.length) {
    ElMessage.warning('请选择要删除的项')
    return
  }
  deleteDialogVisible.value = true
}

// 确认删除
const confirmDelete = () => {
  // TODO: 调用删除接口
  ElMessage.success('删除成功')
  deleteDialogVisible.value = false
  // 重新加载数据
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid) => {
    if (valid) {
      // TODO: 调用保存接口
      ElMessage.success(dialogType.value === 'add' ? '添加成功' : '修改成功')
      dialogVisible.value = false
      // 重新加载数据
    }
  })
}
</script>

<style scoped>
.quality-template {
  .dialog-footer {
    @apply flex justify-end gap-2;
  }
}
</style> 